 /*
  * Copyright (c) 2021, 2023, yishun.cn All rights reserved.
  *
  */
 package cn.shun.controller;

 import cn.shun.mapper.BookMapper;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.cache.annotation.CacheEvict;
 import org.springframework.cache.annotation.Cacheable;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;

 import java.util.*;

 /**
  * <p>Project: MyBatis2023 - IndexController
  * <p>Powered by yishun On 2023-02-03 11:08:42
  *
  * @author yishun [2465734101@qq.com]
  * @version 1.0
  * @since 17
  */
 @RestController
 public class IndexController {
     @Autowired
     private BookMapper bm;
     @RequestMapping @Cacheable(value = "default")
     public Map<String, Object> index() {
         Map<String, Object> map = new HashMap<>();
         map.put("code", 200);
         map.put("msg", "查询成功");
         map.put("data", bm.selectAll());
         return map;
     }
     @RequestMapping("/clear") @CacheEvict(value = "default",allEntries = true)
     public Map<String, Object> clear() {
         Map<String, Object> map = new HashMap<>();
         map.put("code", 200);
         map.put("msg", "缓存数据清除成功");
         return map;
     }
     @RequestMapping("/show") @Cacheable(value = "default",key = "#id")
     public Map<String, Object> show(int id) {
         Map<String, Object> map = new HashMap<>();
         map.put("code", 200);
         map.put("msg","查询成功");
         map.put("data",bm.queryById(id));
         System.out.println("设置id="+id+"的缓存");
         return map;
     }
     @RequestMapping("/cid") @CacheEvict(value = "default",key = "#id")
     public Map<String, Object> cid(int id) {
         Map<String, Object> map = new HashMap<>();
         map.put("code", 200);
         System.out.println("清除id="+id+"的缓存");
         return map;
     }
 }
